package eu.codlab.mouse_client;

import android.app.Fragment;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainFragment extends Fragment{
	private TextView _server_ip;
	private TextView _server_port;
	private Button _server_go;
	private Button _server_write;

	public MainFragment(){
	}

	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View mainView = inflater.inflate(R.layout.launcher, container, false);		
		return mainView;

	}


	@Override
	public void onViewCreated(final View v, Bundle saved){
		_server_ip = (TextView)v.findViewById(R.id.server);
		_server_port = (TextView)v.findViewById(R.id.port);
		
		SharedPreferences p = this.getActivity().getSharedPreferences("MOUSE_PREFS", 0);
		String server = p.getString("ip","");
		String ip = p.getString("port","");
		setServer(server, ip);
		
		_server_go = (Button)v.findViewById(R.id.go);

		_server_go.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				SharedPreferences p = MainFragment.this.getActivity().getSharedPreferences("MOUSE_PREFS", 0);
				p.edit().putString("ip", _server_ip.getText().toString())
				.putString("port", _server_port.getText().toString()).commit();
				((GoActivity) getActivity()).go();
			}

		});
		
		_server_write = (Button)v.findViewById(R.id.write);

		_server_write.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				SharedPreferences p = MainFragment.this.getActivity().getSharedPreferences("MOUSE_PREFS", 0);
				p.edit().putString("ip", _server_ip.getText().toString())
				.putString("port", _server_port.getText().toString()).commit();
				((GoActivity) getActivity()).enableWriteMode();
			}

		}); 
	}

	public void onPause(){
		super.onPause();
	}

	public void onResume(){
		super.onResume();
	}

	public void onDestroy(){
		super.onDestroy();
	}

	public void setServer(String s, String i) {
		_server_ip.setText(s);
		_server_port.setText(i);
	}
}
